﻿
package com.ostrich.manpower.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.DateUtil;
import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.manpower.bean.SendMsg;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrDeductinfo;
import com.ostrich.manpower.bean.TostHrFactpayinfo;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
import com.ostrich.manpower.bean.TostHrSalaryinfo;
import com.ostrich.manpower.bean.TostHrSubsidyinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrDeductinfoService;
import com.ostrich.manpower.service.impl.TostHrFactpayinfoService;
import com.ostrich.manpower.service.impl.TostHrMealaddinfoService;
import com.ostrich.manpower.service.impl.TostHrSalaryinfoService;
import com.ostrich.manpower.service.impl.TostHrSubsidyinfoService;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostHrSalaryinfo")
public class TostHrSalaryinfoController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostHrSalaryinfoController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrSalaryinfoService<TostHrSalaryinfo> tostHrSalaryinfoService; 
	
	@Autowired
	private TostHrMealaddinfoService<TostHrMealaddinfo> tosthrmealaddinfoservice;
	
	@Autowired(required=false)
	private TostHrBaseinfoService<TostHrBaseinfo> tostHrBaseinfoService;
	
	@Autowired(required=false)
	private TostHrFactpayinfoService<TostHrFactpayinfo> tosthrfactpayinfoservice;
	
	@Autowired(required=false)
	private TostHrDeductinfoService<TostHrDeductinfo> tosthrdeductinfoservice;

	@Autowired(required=false)
	private TostHrSubsidyinfoService<TostHrSubsidyinfo> tosthrsubsidyinfoservice;
	
	private List<TostHrSalaryinfo> sessiondataList;
	
	private TostHrSalaryinfo  hrSalaryinfo;
	
	private boolean isSession = false;
	
	private int num = 0;
	
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView  list(TostHrSalaryinfo tostHrSalaryinfo,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = getRootMap();
		List<TostHrSalaryinfo> dataList = tostHrSalaryinfoService.queryByList(tostHrSalaryinfo);
		//设置页面数据
		context.put("dataList", dataList);
		return forword("manpower/tabTostHrSalaryinfo",context); 
	}
	
	/**
	 * json 列表页面
	 * @param urld
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList")
	public void  dataList(TostHrSalaryinfo tostHrSalaryinfo,HttpServletResponse response,HttpServletRequest request,String isLeft) throws Exception{
		HttpSession session  = request.getSession();
		String month=tostHrSalaryinfo.getHsaMonth();
		if(month!=null&&month.length()>0){
			String [] months=month.split("-");
			String year=months[0];
			String m=months[1];
			tostHrSalaryinfo.setHsaMonth(year+m);
		}
		//由于session问题，导致分页有问题
		//防止报空指针
//		tostHrSalaryinfo = (tostHrSalaryinfo!=null)?tostHrSalaryinfo:new TostHrSalaryinfo();
		//session.setAttribute("TostHrSalaryinfo", new TostHrSalaryinfo());
		TostHrSalaryinfo Salaryinfo = (TostHrSalaryinfo) session.getAttribute("TostHrSalaryinfo");
		//将查询的条件放人session中 
		if ((tostHrSalaryinfo.getHrName()!=null && tostHrSalaryinfo.getHrName().length()>0)
				|| (tostHrSalaryinfo.getHsaMonth()!=null&& tostHrSalaryinfo.getHsaMonth().length()>0)) {
		/*	tostHrSalaryinfo.setPage(tostHrSalaryinfo.getPage());
			tostHrSalaryinfo.setPager(tostHrSalaryinfo.getPager());*/
			session.setAttribute("TostHrSalaryinfo", tostHrSalaryinfo);
		}
		//没有填写信息
		else{
			//点击查询
			if (("".equals(tostHrSalaryinfo.getHrName()) ) && ("".equals(tostHrSalaryinfo.getHsaMonth()) )) {
				//tostHrSalaryinfo = tostHrSalaryinfo;
				//session.setAttribute("TostHrSalaryinfo", null);
				session.removeAttribute("TostHrSalaryinfo");
			}
			//点击返回
			else{
				if (Salaryinfo != null) {
					tostHrSalaryinfo = Salaryinfo;
				}
				//session.setAttribute("TostHrSalaryinfo", tostHrSalaryinfo);
				//tostHrSalaryinfo.setPage(tostHrSalaryinfo.getPage());
				//tostHrSalaryinfo.setPager(tostHrSalaryinfo.getPager());
			}
		}
		
		sessiondataList = tostHrSalaryinfoService.queryByMap(tostHrSalaryinfo);
		
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",tostHrSalaryinfo.getPager().getRowCount());
		jsonMap.put("rows", sessiondataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public void save(TostHrSalaryinfo bean,HttpServletRequest request,HttpServletResponse response) throws Exception{
		/*Map<String,Object>  context = new HashMap<String,Object>();
		tostHrSalaryinfoService.updateBySelective(bean);
		sendSuccessMessage(response, "保存成功~");*/
		
		bean.setCreatetime(DateUtil.getNowPlusTime());
		bean.setPerson(SessionUtils.getUser(request).getEmail());
		
		if(bean.getId()==null){
			tostHrSalaryinfoService.add(bean);
		}else{
			tostHrSalaryinfoService.update(bean);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostHrSalaryinfo bean  = tostHrSalaryinfoService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletResponse response) throws Exception{
		//删除工资内容
		tostHrSalaryinfoService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/checkhrCode")
	public void checkHrcode(HttpServletResponse response,String hrCode){
		TostHrBaseinfo thr= tostHrBaseinfoService.getBaseinfoByHrCode(hrCode);
		if(thr!=null&&thr.getId()!=null)
			HtmlUtil.writerJson(response,"true");
		else
			HtmlUtil.writerJson(response,"false");
	}
	
	@RequestMapping("/createtab")
	public void createTab(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		request.setAttribute("month", request.getParameter("month"));
		String month =(String) request.getAttribute("month");
		String hrCode = request.getParameter("hrid");
		int hrid=0;
		try{
			if(hrCode!=null && hrCode.length()>0){
				TostHrBaseinfo thr= tostHrBaseinfoService.getBaseinfoByHrCode(hrCode);
				if(thr!=null)
					hrid = thr.getId();
				else{
					sendSuccessMessage(response,"输入的用户不存在！");
				}
			}
		}catch(Exception e){
			hrid=0;
			System.out.println("hrid=0");
		}
		
		String hs ="";
		if(month!=null && month.length()>8){
			String times12 = month.substring(0,4);
			String times11 = month.substring(5,7);
			hs=times12 + times11;
		}
		//增加工资创建时间跟创建人
		TostHrSalaryinfo bean=new TostHrSalaryinfo();
		bean.setCreatetime(DateUtil.getNowPlusTime());
		bean.setPerson(SessionUtils.getUser(request).getEmail());
		bean.setHsaMonth(hs);
		SendMsg sm=tostHrSalaryinfoService.addDeductFactpaySubsalary(hs,hrid,bean);//第一个参数是月份，第二个事hrId，hrid为0就代表生成全部。
		sendSuccessMessage(response, sm.getMsg());
	}
}
